package com.dongwu.security.entity;

import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.dongwu.common.entity.BaseEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;

@Entity
@Table(name = "sys_role")
public class Role extends BaseEntity<String>{

	@Column(unique=true)
    private String name;
    
    private String remark;
    
    // 父权限  
    @ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)  
    @JoinColumn(name = "parent_id") 
    private Role parent;  
    
    // 子权限  
    @JsonIgnore
    @OneToMany(cascade = CascadeType.REFRESH, mappedBy = "parent", fetch = FetchType.LAZY)  
    private Set<Role> children = new LinkedHashSet<Role>();  

    
    public Role() {
	}

	public Role(String id, String name, String remark) {
		this.setId(id);
		this.name = name;
		this.remark = remark;
	}
    
	public Role(String name, String remark) {
		this.name = name;
		this.remark = remark;
	}

	public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    public String getRemark() {
		return remark;
	}

	public void setRemark(String desc) {
		this.remark = desc;
	}

	public Role getParent() {
		return parent;
	}

	public void setParent(Role parent) {
		this.parent = parent;
	}

	public Set<Role> getChildren() {
		return children;
	}

	public void setChildren(Set<Role> children) {
		this.children = children;
	}

	@Override
	public String toString() {
		return "Role [name=" + name + ", remark=" + remark + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		Role other = (Role) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
}
